package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.entity.Account;
import com.example.entity.Employee;
import com.example.exception.CoustomException;
import com.example.mapper.EmployeeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    public List<Employee> selectAll(Employee employee) {
        List<Employee> list = employeeMapper.selectAll(employee);
        return list;
    }

    public Employee selectById(Integer id) {
        Employee employee = employeeMapper.selectById(id);
        return employee;
    }

    public PageInfo<Employee> selectPage( Employee employee,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeMapper.selectAll(employee);
        return PageInfo.of(list);
    }

    public void add(Employee employee) {
        String username = employee.getUsername();
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if(dbEmployee != null){ //数据库存在这个账号
            throw new CoustomException("500","账号已存在");
        }
        Employee dbEmployee1 = employeeMapper.selectByNo(employee.getNo());
        if(dbEmployee1 != null){ //工号是唯一的
            throw new CoustomException("500","工号已存在");
        }
        if(StrUtil.isBlank(employee.getPassword())){ //密码没填
            employee.setPassword("123"); //默认密码

        }
        if(StrUtil.isBlank(employee.getName())){ //名字没填
            employee.setName(employee.getUsername()); //默认名称
        }
        //一定要设置角色
        employee.setRole("EMP"); //员工角色
        employeeMapper.insert(employee);
    }

    public void update(Employee employee) {
        employeeMapper.updateById(employee);
    }

    public void deleteById(Integer id) {
        employeeMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }

    public Employee login(Account account) {
        String username = account.getUsername();
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if(dbEmployee == null){ //没查询到用户,说明没有这个账号
            throw new CoustomException("500","账号不存在");
        }
        //数据库存在这个账号
        String password = account.getPassword();
        if(!dbEmployee.getPassword().equals(password)){
            throw new CoustomException("500","账号或密码错误");
        }
        return dbEmployee;
    }

    public void register(Employee employee) {
        this.add(employee);
    }

    public void updatePassword(Account account) {
        Integer id = account.getId();
        Employee employee = this.selectById(id);
        if (!employee.getPassword().equals(account.getPassword())) { //页面传来的原密码跟数据库密码相对比,不匹配就报错
            throw new CoustomException("500","对不起,原密码错误");
        }
        employee.setPassword(account.getNewPassword());//设置新密码
        this.update(employee);
    }
}
